package com.magicode.screen.react.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;

import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.magicode.screen.AppContext;

/**
 * Created by wuxt on 2016/8/24.
 */
public abstract class MgcReactActivity extends ReactActivity {

    protected ReactInstanceManager mReactInstanceManager = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();

        ReactNativeHost rnh = AppContext.getInstance().getReactNativeHost();
        if (null != rnh) {
            mReactInstanceManager = rnh.getReactInstanceManager();
        }
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
    }
    @Override
    protected void onPause() {
        super.onPause();

        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostPause();
        }
    }

    @Override
    public void onBackPressed() {
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onBackPressed();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager.showDevOptionsDialog();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
  //  @Override
    protected boolean getUseDeveloperSupport() {
        return false;
    }
//    @Override
//    public void invokeDefaultOnBackPressed() {
//        super.onBackPressed();
//    }
    /**
     * 向JS发送事件
     * @param reactContext
     * @param eventName
     * @param params
     */
    protected void sendEvent(ReactContext reactContext,
                           String eventName,
                           @Nullable WritableMap params) {
        if (reactContext==null) {

        }else{
            reactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit(eventName, params);
        }

    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }
//    protected List<ReactPackage> getPackages() {
//        return Arrays.<ReactPackage>asList(
//                new MainReactPackage(),new MgcReactPackage()); // <-- 添加这一行，类名替换成你的Package类的名字.
//    }

}
